package com.pic.mgt.business.operation;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.file.io.FileOperation;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.syushin.executer.bucket.SyushinBucketProcess;
import com.pic.mgt.utils.FilePathUtils;
import com.pic.mgt.utils.LogUtils;

public class SimpleBusinessOperation implements BusinessOperation {
	private String syushinBatchTmpPathStr;
	private FileOperation fileOperation;
	
	@Override
	public File createBatchBusinessTmpFolder(String businessName)
			throws BusinessOperationException {
		File businessBucketFolder = FilePathUtils.multiPathConcat(
												this.syushinBatchTmpPathStr,
												businessName,
												SyushinBucketProcess.bucketNameUnderBusinessFolder);

		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this,
						"Business '{}' operation: creating folder '{}'.",
						businessName, businessBucketFolder
						.getAbsolutePath());
			}
			// ********************** Log **********************

			if (!businessBucketFolder.exists()) {
				this.fileOperation.createFolder(businessBucketFolder);
			}

			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this,
						"Business '{}' operation: creating folder '{}' completed.",
						businessName, businessBucketFolder
						.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new BusinessOperationException("Business '"
						+ businessName
						+ "' operation: creating folder '"
						+ businessBucketFolder.getAbsolutePath() + "' failed.", e);
			} catch (BusinessOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************

				throw e1;
			}
		}

		return businessBucketFolder;
	}

	@Override
	public File removeBatchBusinessTmpFolder(String businessName)
			throws BusinessOperationException {
		File businessFolder = new File(this.syushinBatchTmpPathStr, businessName);
		
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog.debug(this,
						"Business '{}' operation: removing business '{}'.",
						businessName, businessFolder.getAbsolutePath());
			}
			// ********************** Log **********************
			
			if (businessFolder.exists()) {
				this.fileOperation.deleteFile(businessFolder);
			}

			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isDebugMode()) {
				IgxLog.messageDetailsLog
						.debug(
								this,
								"Business '{}' operation: removing folder '{}' completed.",
								businessName,
								businessFolder.getAbsolutePath());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			try {
				throw new BusinessOperationException("Business '"
						+ businessName
						+ "' operation: removing folder '"
						+ businessFolder.getAbsolutePath() + "' failed.", e);
			} catch (BusinessOperationException e1) {
				// ********************** Log **********************
				LogUtils.error(this, IgxLog.messageDetailsLog, e1);
				// ********************** Log **********************

				throw e1;
			}
		}
		
		return businessFolder;
	}
	
	// --- getter and setter ---

	public void setSyushinBatchTmpPathStr(String syushinBatchTmpPathStr) {
		this.syushinBatchTmpPathStr = syushinBatchTmpPathStr;
	}

	public void setFileOperation(FileOperation fileOperation) {
		this.fileOperation = fileOperation;
	}

}
